package collectors.calendar.util;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public final class DateUtil implements Serializable {

  private static final long serialVersionUID = 1L;

  private static final TimeZone GMT = TimeZone.getTimeZone("GMT");

  
  public static Date parseToDate(String str) {
    try {
        int year = Integer.parseInt(str.substring(0, 4));
        int month = Integer.parseInt(str.substring(5, 7)) - 1;
        int day = Integer.parseInt(str.substring(8, 10));
        int length = str.length();
        boolean dateOnly = length <= 10 || Character.toUpperCase(str.charAt(10)) != 'T';
        int hourOfDay = 0;
        int minute = 0;
        int second = 0;
        int milliseconds = 0;
        if (!dateOnly) {
          hourOfDay = Integer.parseInt(str.substring(11, 13));
          minute = Integer.parseInt(str.substring(14, 16));
          second = Integer.parseInt(str.substring(17, 19));
          if (str.charAt(19) == '.') {
            milliseconds = Integer.parseInt(str.substring(20, 23));
          } else {
          }
        }
        Calendar dateTime = new GregorianCalendar(GMT);
        dateTime.set(year, month, day, hourOfDay, minute, second);
        dateTime.set(Calendar.MILLISECOND, milliseconds);
        return dateTime.getTime();
        
      } catch (StringIndexOutOfBoundsException e) {
    	  return null;
    	  //throw new NumberFormatException("Invalid date/time format: " + str);
      }
  }
 
}

